package com.example.secondhandspringboot.service.impl;

import com.example.secondhandspringboot.context.BaseContext;
import com.example.secondhandspringboot.domain.entity.Comments;
import com.example.secondhandspringboot.domain.vo.CommentsVO;
import com.example.secondhandspringboot.mapper.CommentsMapper;
import com.example.secondhandspringboot.service.ICommentsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 周在林
 * @since 2024-04-07
 */
@Service
@RequiredArgsConstructor
public class CommentsServiceImpl extends ServiceImpl<CommentsMapper, Comments> implements ICommentsService {
    private final CommentsMapper commentsMapper;

    /**
     * 根据商品id获取评论
     * @param goodsId
     * @return
     */
    @Override
    public List<CommentsVO> getComments(Long goodsId,Long sellerId) {
        List<CommentsVO> commentsByGoodsId = commentsMapper.getCommentsByGoodsId(goodsId);
        //判断评论为商家还是本人
        List<CommentsVO> updateList = commentsByGoodsId.stream().peek(comment -> {
            comment.setSeller(Objects.equals(comment.getUserId(), sellerId));
            comment.setSelfOne(Objects.equals(comment.getUserId(), BaseContext.getCurrentId()));
        }).toList();
        return updateList;
    }
}
